#include "subtract_op.h"
#include "../constant.h"
#include "multiply_op.h"
#include "../../factory/factory.h"
#include "../../factory/constant_factory.h"
#include "../../factory/multiply_factory.h"

Subtract::Subtract(Node* lhs, Node* rhs) : OpNode({lhs, rhs}){
}

vector<Node*> Subtract::getGradNodes(Node* grad){
    Factory* factory;
    Shape* shape = grad -> getOutShape();
    factory = new ConstantFactory(Matrix(shape -> rows, shape -> cols, -1));
    Node* neg_ones = factory -> createNode();
    delete factory;
    factory = new MultiplyFactory(grad, neg_ones);
    Node* r_grad = factory -> createNode();
    return {grad, r_grad};
}

void Subtract::compute(Context* context){
    auto nodes = *getNodeIn();
    auto l_mat = context -> getFlowedData(nodes[0]);
    auto r_mat = context -> getFlowedData(nodes[1]);
    context -> setFlowedData(this, l_mat - r_mat);
}
